<?php
//用户金币
class Controller_Admin_UserV2Coin extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'userCoin';
        $this->_list_pagesize = 20;
        parent::__construct($req, $res);

        $this->_list_where = array(
        );

        $this->_sort_extra_columns = array(
        );
    }

	//默认列表首页
    public function action_index ()
    {
        $this->action_list();
    }

	//修改用户金币数
	//$nid int 用户id
    public function action_update ($nid)
    {
        $uid  = $nid;
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;

            $coins = Arr::get($post, 'coin', 0);
            if ($coins != 0)
            {
                App_Account::addAccountCoin($uid, $coins, "后台管理员操作");
            }

            $this->to_url('list');
        }
        else
        {
            App_Account::initAccount($uid);
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }

        parent::action_update($nid);

    }

    // 金币流水
    public function action_history ($nid)
    {
        $this->_form_type = 'userCoinHistory';
        $this->_list_pagesize = 10;
        $this->_list_where = array(
            array('uid', '=', $nid),
        );

        $this->action_list(1);
    }

	//金币详细信息
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        $this->_form_data['userIcon'] = explode('|', $data['userIcon']);
        parent::action_detail($nid);
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

    private function rebuild_post_data (& $post)
    {
        //
    }

	//菜单列表
    public function actions_by_list_row (& $row, & $menus)
    {
        array_pop($menus);
        array_pop($menus);
        array_pop($menus);
        $menus[] = '<a href="/admin/userv2coin/update/'.$row['uid'].'">添加/扣减金币数</a>';
        $menus[] = '<a href="/admin/userv2coinhistory/list?uid='.$row['uid'].'">金币流水</a>';
    }

	//账户信息
    public function list_get_data ($offset, $limit)
    {
        $infos = App_Account::infoList($offset, $limit, TRUE);
        return $infos;
    }

	//重构数据
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            switch ($rs['source'])
            {
                case 'Wechat':
                    $rs['source'] = '微信';
                    break;
                case 'QQ':
                    $rs['source'] = 'QQ';
                    break;
                case 'SinaWeibo':
                    $rs['source'] = '新浪';
                    break;

                case 'mobile':
                    $rs['source'] = '手机';
                    break;

                default: 
                    $rs['source'] = '';
                    break;
            }

            if (empty($rs['coin']))
                $rs['coin'] = 0;
        }
    }
}
